﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using HZ.MBSM.DBUtility;

public partial class Manage_ProductUpdate : System.Web.UI.Page
{

    protected void Page_Load(object sender, EventArgs e)
    {
        System.Collections.Generic.List<HZ.MBSM.DAL.AccountType> _AccountType = new System.Collections.Generic.List<HZ.MBSM.DAL.AccountType>();
        _AccountType.Add(HZ.MBSM.DAL.AccountType.enumAdmin);
        _AccountType.Add(HZ.MBSM.DAL.AccountType.enumAttorney);
        _AccountType.Add(HZ.MBSM.DAL.AccountType.enumCheckGoods);
        if (HZ.MBSM.BLL.Login.CheckAssert(_AccountType))
        {
            if (!IsPostBack)
            {
                Int64 ProductId;
                string strSql;
                ProductId = Convert.ToInt64(SysFun.IsSQL(Request.QueryString["ProductId"].ToString()));
                Db db = new Db();
                System.Data.IDataReader dr;
                strSql = "select product.*,product_sort.sortName from product,product_sort where product.sortid=product_sort.sortid and  ProductId=" + ProductId;
                dr = db.DataReader(strSql);
                while (dr.Read())
                {
                    txtMedicalCareCode.Text = SysFun.ToTrim(dr["MedicalCareCode"]);
                    txtMedicinemodel.Text = SysFun.ToTrim(dr["Medicinemodel"]);
                    txtOutlookC.Text = SysFun.ToTrim(dr["OutlookC"]);
                    txtProductCode.Text = SysFun.ToTrim(dr["ProductCode"]);                    
                    txtProductId.Text = SysFun.ToTrim(dr["ProductId"]);
                    txtProductId.Visible = false;
                    txtProductName.Text = SysFun.ToTrim(dr["ProductName"]);
                    txtRemark.Text = SysFun.ToTrim(dr["Remark"]);

                    txtSortId1.Text = SysFun.ToTrim(dr["SortId"]);
                    txtSortId1.Visible = false;
                    txtSortName.Text = SysFun.ToTrim(dr["sortName"]);
                    txtUnit.Text = SysFun.ToTrim(dr["Unit"]);
                    chkIsMedicalCare.Checked = SysFun.ToBool(dr["IsMedicalCare"]);
                    txtAddDate.Text = SysFun.ToTrim(dr["AddDate"]);
                    txtAddDate.Visible = false;
                    txtAccountId.Text = SysFun.ToTrim(dr["AccountId"]);
                    txtAccountId.Visible = false;
                    txtXiShu.Text = SysFun.ToTrim(dr["xishu"]);
                    txtZL.Text = SysFun.ToTrim(dr["zl"]);
                    txtHL.Text = SysFun.ToTrim(dr["hl"]);

                }
                dr.Close();
                dr.Dispose();
                db.Close();
            }
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        HZ.MBSM.Model.Product Product = new HZ.MBSM.Model.Product();
        string SortId;
        SortId = SysFun.ToTrim(Request.Form["txtSortId"]);
        if (SortId == "")
        {
            SortId = txtSortId1.Text.ToString();
        }

        HZ.MBSM.Model.Account Ac = (HZ.MBSM.Model.Account)Session["Account"];
        if (Ac != null)
        {
            int rowCun = 0;
            Db db1 = new Db();
            System.Data.IDataReader dr;
            string strSql = "select count(productid) as cun from product where  (productcode='" + SysFun.ToTrim(txtProductCode.Text) + "' or ProductName='" + SysFun.ToTrim(txtProductName.Text) + "'  AND  Outlookc= '" + SysFun.ToTrim(txtOutlookC.Text) + "' AND Medicinemodel='" + SysFun.ToTrim(txtMedicinemodel.Text) + "' and xishu=" + SysFun.ToTrim(txtXiShu.Text) + ") and productid <>" + SysFun.ToLong(txtProductId.Text);
            dr = db1.DataReader(strSql);
            if (dr.Read())
            {
                rowCun = SysFun.ToInt(dr["cun"]);
            }
            dr.Close();
            dr.Dispose();
            db1.Close();
            if (rowCun > 0)
            {
                Response.Write("<script language=JavaScript>alert('该商品在数据库中已经存在！！！');</script>");

            }
            else
            {

                Product.ProductId = SysFun.ToLong(txtProductId.Text);
                Product.AccountId = SysFun.ToLong(txtAccountId.Text);
                if (txtAddDate.Text.Trim() == "")
                {
                    Product.AddDate = Convert.ToDateTime("1900-1-1");
                }
                else
                {
                    Product.AddDate = Convert.ToDateTime(txtAddDate.Text);
                }

                Product.IsMedicalCare = chkIsMedicalCare.Checked;
                Product.MedicalCareCode = txtMedicalCareCode.Text;
                Product.Outlookc = txtOutlookC.Text;
                Product.ProductName = txtProductName.Text;
                Product.Remark = txtRemark.Text;
                Product.SortId = SortId;
                Product.Medicinemodel = txtMedicinemodel.Text;
                Product.SpellingCode = HZ.MBSM.BLL.PYM.GetChineseSpell(Product.ProductName);
                Product.Unit = txtUnit.Text;
                Product.ProductCode = txtProductCode.Text;
                Product.XiShu = SysFun.ToInt(txtXiShu.Text);
                Product.HL = SysFun.ToFloat(txtHL.Text);
                Product.ZL = SysFun.ToFloat(txtZL.Text);
                HZ.MBSM.BLL.Product ProductAction = new HZ.MBSM.BLL.Product();
                ProductAction.UpdateProduct(Product);

                Response.Write("<script>alert('保存成功！！！');</script>");

                HZ.MBSM.Model.Account _Account = (HZ.MBSM.Model.Account)Session["Account"];
                HZ.MBSM.BLL.LogManage.RecordLoginDetail(_Account.AccountName, Request.UserHostAddress, "更新", "Product", Product.ProductName + "|" + Product.ProductCode + "|" + Product.MedicalCareCode + "|" + Product.Medicinemodel + "|" + Product.Outlookc + "|" + Product.SortId + "|" + Product.XiShu, "../Magage/ProductUpdate.aspx");


                Response.Write("<script>window.opener.location.reload();window.close();</script>");
            }
        }
        else
        {
            Response.Write("<script language=JavaScript>alert('您无权修改商品！！！');</script>");
        }

    }


    protected void Button2_Click(object sender, EventArgs e)
    {
        Response.Write("<script>window.close();</script>");
    }
}
